import {useRouter, useRoute, } from "vue-router";

/**
 * 跳转登录页面
 */
function jumpLoginPage(){ //
    console.log('跳转登录接口')
}

function jumpNoPermissionPage(){
    const {router,route} = useRouterAndRoute();
    console.log('jumpNoPermissionPage-router', router, route)
}

function useRouterAndRoute() {
    const router = useRouter();
    const route = useRoute() as any;
    return { router, route };
}
interface MenuItem {
    path: string;
    name: string;
    icon: string;
    children?: MenuItem[];
}

function throttle(func: (...args: any[]) => void, delay: number): (...args: any[]) => void {
    let timerId: ReturnType<typeof setTimeout> | undefined;
    let lastExecutedTime = 0;

    return function (this: any,...args: any[]) {
        const currentTime = Date.now();

        if (currentTime - lastExecutedTime > delay) {
            lastExecutedTime = currentTime;
            func.apply(this, args);
        } else {
            if (timerId) {
                clearTimeout(timerId);
            }

            timerId = setTimeout(() => {
                lastExecutedTime = currentTime;
                func.apply(this, args);
            }, delay);
        }
    };
}

// 包含文件类型的深拷贝
function deepCopyWithFiles(obj: any) {
    // Handle special cases like files
    if (obj instanceof File) {
        return obj; // Skip copying files
    }

    if (obj === null || typeof obj !== 'object') {
        return obj;
    }
    let copy: any = Array.isArray(obj) ? [] : {};
    for (let key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
            copy[key] = deepCopyWithFiles(obj[key]);
        }
    }

    return copy;
}


function debounce(func: (...args: any[]) => void, delay: number): (...args: any[]) => void {
    let timeoutId: ReturnType<typeof setTimeout> | undefined;

    return function(this: any, ...args: any[]) {
        if (timeoutId) {
            clearTimeout(timeoutId);
        }

        timeoutId = setTimeout(() => {
            func.apply(this, args);
        }, delay);
    };
}

function uploadFormatPreview(fileUrl:string | string[], name:string = '' ){
    return [{url:fileUrl, name}]
}

/**
 * @description 截取url中最后"\"内容，及文件名称
 * @param url
 */
function extractFileName(url:string): {
    fileName: string;
    fileFormat: string;
    allName: string;
} | null  {
    const startIndex = url.lastIndexOf('/') + 1; // 获取文件名开始的索引
    const endIndex = url.lastIndexOf('.'); // 获取文件名结束的索引
    if (startIndex >= endIndex) {
        return null; // URL 不包含文件名或格式
    }
    const fileName = url.substring(startIndex, endIndex); // 提取文件名部分
    const fileFormat = url.substring(endIndex + 1); // 提取文件格式部分
    const allName = fileName + fileFormat
    return { fileName, fileFormat, allName };
}


/**
 * @description 将菜单数组扁平化
 * @param menuList
 */
function flattenMenuList(menuList: MenuItem[]): MenuItem[] {
    const flattenedList: MenuItem[] = [];
    for (const item of menuList) {
        flattenedList.push(item);

        if (item.children && item.children.length > 0) {
            const flattenedChildren = flattenMenuList(item.children);
            flattenedList.push(...flattenedChildren);
        }
    }

    return flattenedList;
}

export {
    debounce,
    deepCopyWithFiles,
    flattenMenuList,
    uploadFormatPreview,
    extractFileName,
    useRouterAndRoute,
    jumpNoPermissionPage,
    jumpLoginPage,
    throttle
}